package com.topscomm.mis.cbb.pm.service.chain.handler;

import com.topscomm.mis.cbb.pm.api.ITaskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskService;
import com.topscomm.mis.cbb.pm.base.service.impl.CbbPmTaskService;
import com.topscomm.mis.cbb.pm.service.chain.TaskCompleteHandler;
import com.topscomm.mis.cbb.pm.util.PmSystemConst;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import java.util.Date;
import java.util.Map;

/**
 * @description: 责任链会签任务处理
 * @author: wangxinhua
 * @date: 2023/6/16 10:07
 * @modify:
 */
@Scope(value = "singleton")
public class CountersignHandler extends TaskCompleteHandler {

    @Override
    public void completeTask(Map<String, Object> taskInfo) {
        CbbPmTaskService cbbPmTaskService =  (CbbPmTaskService) SpringUtil.getBean("cbbPmTaskService");
        if (PmSystemConst.TaskRelation.COUNTERSIGN == ConvertUtil.convertToInt(taskInfo.get(CbbPmTaskEntity.FieldRelation))) {
            //处理会签类型任务，会签类型完成当前任务，后获取所有本级任务是否关闭，若都关闭则返回true
            //更新任务状态
            taskInfo.put(CbbPmTaskEntity.FieldStatus, PmSystemConst.TaskStatus.COMPLETE);
            taskInfo.put(CbbPmTaskEntity.FieldRealendtime, new Date());
            taskInfo.put(CbbPmTaskEntity.FieldId, taskInfo.get(CbbPmTaskEntity.FieldId));
            cbbPmTaskService.update(taskInfo);
        } else {
            this.getNextHandler().completeTask(taskInfo);
        }
    }
}
